''' 
RWIG. Reverse Wrapper and Interface Generator for Embedding Python into C.

    usage: %%prog [Options]
    
    options:
    string -i, --interface: Name of the interface file.
    string -o, --output: Location to create generated source files. Created if doesn't exist.
    flag -f, --frozen: Freeze python into assembly. (not supported)
    flag -O, --optimize: Optimize python scripts (__debug__=False). (not supported)
    flag -Z, --optimize2: Same as -O but remove doc strings as well. (not supported)
    
    version: RWIG %s
'''

# Copyright 2007 Eloff Enterprises
#
# Licensed under the BSD License (the "License").
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://rwig.googlecode.com/files/LICENSE
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.  See the License for the specific language governing
# permissions and limitations under the License.
#
# Originally developed by Daniel Eloff.

__url__ = 'http://code.google.com/p/rwig/'
__author__ = 'Daniel Eloff'
__version__ = '0.1'

__doc__ %= __version__

import sys
import os
import optionparse
import shutil

from interface import RWIGInterface
import generation

SRC_DIR = os.path.dirname(__file__)

def main():
    opts = optionparse.parse(__doc__)
    
    if not opts.interface:
        return 'You must supply an RWIG interface file (-i option)!'
    
    output_dir = os.path.abspath(os.path.curdir)
    if opts.output:
        if os.path.isabs(opts.output):
            output_dir = opts.output
        else:
            output_dir = os.path.join(output_dir, opts.output)
            
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    else:
        for fname in os.listdir(output_dir):
            os.remove(os.path.join(output_dir, fname))
        
    interface = RWIGInterface(opts.interface)
    print 'Generating code:'
    generation.generate_cpp(interface.modules, output_dir)
    for s in ['rwig_utils.h', 'rwig_utils.c']:
        print 'Copying', s
        shutil.copy(os.path.join(SRC_DIR, s), os.path.join(output_dir, s))
        
    print 'Done.'

if __name__ == '__main__':
    err = main()
    if err:
        print >> sys.stderr, err